package com.example.service.impl;

import com.example.mapper.TypetemplateMapper;
import com.example.pojo.TypeTemplate;
import com.example.pojo.TypeTemplateExample;
import com.example.req.TypeTemplateReq;
import com.example.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class TemplateServiceImpl implements TemplateService {
    @Autowired
    private TypetemplateMapper typetemplateMapper;

    @Override
    public List<TypeTemplate> queryByCondition(TypeTemplateReq typeTemplateReq) {
        TypeTemplateExample example = new TypeTemplateExample();
        TypeTemplateExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(typeTemplateReq.getName())){
            criteria.andNameLike("%" + typeTemplateReq.getName() + "%");
        }
        if (!ObjectUtils.isEmpty(typeTemplateReq.getBrandIds())){
            criteria.andBrandIdsLike("%" + typeTemplateReq.getBrandIds() + "%");
        }
        return typetemplateMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(TypeTemplate typeTemplate) {
        if (typeTemplate.getId() != null){
            return typetemplateMapper.updateByPrimaryKey(typeTemplate);
        }else {
            return typetemplateMapper.insert(typeTemplate);
        }
    }

    @Override
    public int deleteById(Long id) {
        return typetemplateMapper.deleteByPrimaryKey(id);
    }
}
